package o;

import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import androidx.annotation.NonNull;

public final class hw implements OnPreDrawListener, OnAttachStateChangeListener {
    private final Runnable ˊ;
    private ViewTreeObserver ˎ;
    private final View ॱ;

    private hw(View view, Runnable runnable) {
        this.ॱ = view;
        this.ˎ = view.getViewTreeObserver();
        this.ˊ = runnable;
    }

    @NonNull
    public static hw ˎ(@NonNull View view, @NonNull Runnable runnable) {
        if (view == null) {
            throw new NullPointerException("view == null");
        } else if (runnable == null) {
            throw new NullPointerException("runnable == null");
        } else {
            hw hwVar = new hw(view, runnable);
            view.getViewTreeObserver().addOnPreDrawListener(hwVar);
            view.addOnAttachStateChangeListener(hwVar);
            return hwVar;
        }
    }

    public boolean onPreDraw() {
        ˏ();
        this.ˊ.run();
        return true;
    }

    public void ˏ() {
        if (this.ˎ.isAlive()) {
            this.ˎ.removeOnPreDrawListener(this);
        } else {
            this.ॱ.getViewTreeObserver().removeOnPreDrawListener(this);
        }
        this.ॱ.removeOnAttachStateChangeListener(this);
    }

    public void onViewAttachedToWindow(View view) {
        this.ˎ = view.getViewTreeObserver();
    }

    public void onViewDetachedFromWindow(View view) {
        ˏ();
    }
}
